<?php

namespace App\Http\Controllers;

use App\Models\Department;
use App\Services\CommonService;
use App\Services\DepartmentService;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{
    public $departmentService;

    public function __construct(DepartmentService $service)
    {
        parent::__construct();
        $this->departmentService = $service;
    }

    /**
     * 数据列表
     */
    public function index(Request $request)
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->departmentService->getList($perPage);

        return $this->success($list);
    }

    /**
     * 添加部门
     */
    public function store(Request $request)
    {
        $id = $request->input('id');
        if ($id) {
            if (!$this->departmentService->updateData($id, $request->all())) {
                return $this->error('修改失败！');
            }
            CommonService::addActionLog('修改部门', "数据ID:{$id}");
            return $this->success('', '修改成功！');
        } else {
            $userId = user_id();
            $isCompanyAdminer = $this->departmentService->isCompanyAdminer($userId, $request->input('company_id'));
            if (!$isCompanyAdminer && !is_super_admin()) {
                return $this->error('不是公司管理员，无权添加~');
            }
            if (!$departmentId = $this->departmentService->addNewData($request->all())) {
                return $this->error('添加失败~');
            }
            CommonService::addActionLog('添加部门', "数据ID:{$departmentId}");
        }
        return $this->success('', '添加成功！');
    }

    /**
     * 查询单个数据
     */
    public function show(Request $request)
    {
        $id = $request->input('id');
        $info = Department::query()->where('id', $id)->first();
        if (empty($info)) {
            return $this->error('部门不存在！');
        }
        $data = $info->toArray();

        return $this->success($data);
    }

    /**
     * 获取部门下的员工
     */
    public function users(Request $request)
    {
        $departmentId = $request->input('department_id');

        $list = $this->departmentService->getUsers($departmentId);

        return $this->success($list);
    }

    /**
     * 回收站列表（进行软删除的数据）
     */
    public function recycle(Request $request)
    {
        $data = $this->service->classifyRecycle($request);

        return $this->success($data);
    }

    /**
     * 单个软删除 （软删除相关的全部是Laravel内置操作方法）
     */
    public function destroy(Request $request)
    {
        $id = $request->input('id');
        if (!Department::query()->where('id', $id)->delete()) {
            return $this->error('删除失败！');
        };
        CommonService::addActionLog('软删除部门', "数据ID:{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 批量软删除，ids可以是以","逗号分割的字符串，也可以是数组形式
     */
    public function destroyMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Department::destroy($ids)) {
            return $this->error('删除失败！');
        };
        $ids = implode(',', $ids);
        CommonService::addActionLog('软删除部门', "数据ID:{$ids}");

        return $this->success('', '放入回收站成功');
    }

    /**
     * 恢复单个数据
     */
    function retrieve(Request $request)
    {
        $id = $request->input('id', 0);
        if (!Department::query()->where('id', $id)->restore()) {
            return $this->error('恢复失败！');
        }
        CommonService::addActionLog('恢复单个部门', "数据ID:{$id}");

        return $this->success('', '恢复成功');
    }

    /**
     * 恢复批量数据
     */
    function retrieveMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Department::query()->whereIn('id', $ids)->restore()) {
            return $this->error('恢复失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog('恢复批量部门', "数据ID:{$ids}");

        return $this->success('', '恢复成功');
    }

    /**
     * 永久删除一条数据
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (!Department::withTrashed()->find($id)->forceDelete()) {
            return $this->error('删除失败！');
        }
        CommonService::addActionLog('永久删除部门', "数据ID:{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 永久删除批量数据，ids可以是以","逗号分割的字符串，也可以是数组形式
     */
    public function deleteMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Department::withTrashed()->whereIn('id', $ids)->forceDelete()) {
            return $this->error('删除失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog('永久删除部门', "数据ID:{$ids}");

        return $this->success('', '删除成功');
    }
}
